/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frame.java
 *
 * Created on Mar 21, 2011, 10:26:46 AM
 */

package javaapplication1;

import java.awt.Component;
import java.util.Random;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author davideb
 */
public class frame extends javax.swing.JFrame {

    boolean bandera=false;
    int conta=0;
    Vector <proceso>v=new Vector<proceso>();

    /** Creates new form frame */
    public frame() {
        initComponents();
        this.setBounds(10, 10, 600, 600);

        Random r=new Random(10);
        bandera=false;



        
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        ejecucion = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        listos = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        dormidos = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);
        getContentPane().add(ejecucion);
        ejecucion.setBounds(230, 90, 170, 30);

        jScrollPane1.setViewportView(listos);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(70, 190, 140, 170);

        jScrollPane2.setViewportView(dormidos);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(420, 180, 150, 170);

        jScrollPane3.setViewportView(jList1);

        getContentPane().add(jScrollPane3);
        jScrollPane3.setBounds(430, 380, 140, 130);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(130, 30, 65, 27);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        proceso p=new proceso(this,""+conta,1,6,2);
        v.add((proceso)p);
        p.start();
        conta++;
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frame().setVisible(true);
            }
        });
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JList dormidos;
    protected javax.swing.JTextField ejecucion;
    private javax.swing.JButton jButton1;
    private javax.swing.JFrame jFrame1;
    protected javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    protected javax.swing.JList listos;
    // End of variables declaration//GEN-END:variables




    
    class proceso extends Thread{
        
        String id;
        int quantum;
        int total;
        int recursos;
        frame f;
        
        public proceso(frame f_,String id_,int q,int t, int r){
            
            super(id_);
            id=id_;
            quantum=q;
            total=t;
            recursos=r;
            f=f_;

        }

        @Override
        public void run(){

            while(total>0)
            {
                while(bandera);
                bandera=true;
                if(total<quantum)
                    quantum=total;
                try {
                    System.out.println("hola: "+id+" q:"+quantum);
                    sleep(quantum * 1000);
                    bandera=false;
                    total-=quantum;
                } catch (InterruptedException ex) {
                    Logger.getLogger(frame.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        }


    }



}
